package com.googlecode.grt192.HH11.deploy;

import javax.swing.JFrame;

import com.googlecode.grt192.HH11.actuator.PneumaticActuatorDisplay;
import com.googlecode.grt192.HH11.sensor.SimSwitch;
import com.googlecode.grt192.HH11.sensor.SwitchDisplay;
import com.googlecode.grtframework.vis.Displayer;
import com.googlecode.grtframework.vis.MountedPosition;
import com.googlecode.grtframework.vis.RootMount;

public class MainRobot {
	public static final String CRIO_ADDRESS = "10.1.92.2";
	public static final int CRIO_RPC_PORT = 180;

	public static final String USB_PORT = "/dev/ttyACM0";

	public static final boolean USING_USB = false;

	public static final int NUM_MECHS = 8;

	/**
	 * Buttons
	 */
	// For all arrays: 0-7 are for mechanisms, 8 and 9 are for auto and manual
	public static final int AUTO_BUTTON = 8;
	public static final int MASTER_BUTTON = 9;

	public static final String[] MECHANISM_NAMES = new String[] { "0", "1",
			"2", "3", "4", "5", "6", "7", "auto", "master" };

	public static final int X = 0;
	public static final int Y = 1;
	public static final int[][] SWITCH_POSITIONS = new int[][] { { 350, 150 },
			{ 375, 200 }, { 325, 200 }, { 250, 200 }, { 200, 200 },
			{ 125, 200 }, { 75, 200 }, { 100, 150 }, { 300, 100 }, { 100, 100 } };
	public static final int[] SWITCH_RPC_KEYS = new int[] { 5, 6, 7, 8, 9, 10,
			11, 12, 13, 14, 15 };
	// public static int SWITCH_RPC_KEYS =

	/**
	 * Actuators
	 */
	public static final int[][] ACTUATOR_POSIITIONS = new int[][] { { 50, 40 },
			{ 50, 70 }, { 50, 100 }, { 50, 130 }, { 50, 160 }, { 50, 190 },
			{ 50, 220 }, { 50, 250 } };
	public static final double[] ACTUATOR_ORIENTATION = new double[] {
			Math.PI / 9, Math.PI / 9, Math.PI / 9, Math.PI / 9, Math.PI / 9,
			Math.PI / 9, Math.PI / 9, Math.PI / 9 };
	public static final int CHARACTERSTIC_LENGTH = 0;
	public static final int CHARACTERSTIC_WIDTH = 1;
	public static final int CHARACTERSTIC_EXTEND_LENGTH = 2;
	public static final int CHARACTERSTIC_RETRACT_TIME = 3;
	public static final int CHARACTERSTIC_CONTRACT_TIME = 4;
	public static final int[][] ACTUATOR_CHARACTERISTICS = new int[][] {
			{ 200, 20, 200, 1, 2 }, { 300, 20, 200, 1, 2 },
			{ 300, 20, 200, 1, 2 }, { 300, 20, 200, 1, 2 },
			{ 300, 20, 200, 1, 2 }, { 300, 20, 200, 1, 2 },
			{ 300, 20, 200, 1, 2 }, { 300, 20, 200, 1, 2 } };
	public static final boolean[] ACTUATOR_DEFAULT_STATE = { true, true, true,
			true, true, true, true, true };
	public static final int[] ACTUATOR_RPC_KEYS = { 193, 194, 195, 196, 197,
			198, 199, 200 };
	public static final int[][] MECHANISM_WAIT_TIMES =
	// floor 1
	{ { 1000, 1000, 1000, 1000 },// axes
			{ 1000, 1000, 1000, 1000 },// skull
			{ 1000, 1000, 1000, 1000 },// chair
			{ 1000, 1000, 1000, 1000 },// rat
			// 2nd story
			{ 1000, 1000, 1000, 1000 },// caudron
			{ 1000, 1000, 1000, 1000 },// dog
			{ 1000, 1000, 1000, 1000 },// skeleton
			{ 1000, 1000, 1000, 1000 } };// clock

	public static void main(String[] args) {
		/**
		 * House Display Display
		 */
		Displayer houseDisplay = new Displayer();

		JFrame house = new JFrame("Haunted House 2011: House");
		house.getContentPane().add(houseDisplay);
		house.setVisible(true);
		house.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		/**
		 * Switch Display
		 */
		Displayer switchDisplay = new Displayer();

		JFrame switchboard = new JFrame("Haunted House 2011: Switchboardv1");
		switchboard.getContentPane().add(switchDisplay);
		switchboard.setVisible(true);
		switchboard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// ******************************* SWITCHES

		/**
		 * Master button: started and initialized
		 */
		MountedPosition masterButtonPos = new MountedPosition(RootMount.get(),
				SWITCH_POSITIONS[MASTER_BUTTON][X],
				SWITCH_POSITIONS[MASTER_BUTTON][Y], 0);
		SimSwitch masterButton = new SimSwitch(switchDisplay, masterButtonPos);
		// RPCSwitch masterRPC = new RPCSwitch(usbConn,
		// BUTTON_RPC_KEYS[MASTER_BUTTON]);
		SwitchDisplay masterButtonDisplay = new SwitchDisplay(masterButton,
				masterButtonPos, MECHANISM_NAMES[MASTER_BUTTON]);

		/**
		 * Auto Button: started and initialized
		 */
		MountedPosition autoButtonPos = new MountedPosition(RootMount.get(),
				SWITCH_POSITIONS[AUTO_BUTTON][X],
				SWITCH_POSITIONS[AUTO_BUTTON][Y], 0);
		SimSwitch autoButton = new SimSwitch(switchDisplay, autoButtonPos);
		// RPCSwitch autoRPC = new RPCSwitch(usbConn,
		// BUTTON_RPC_KEYS[AUTO_BUTTON]);
		SwitchDisplay autoButtonDisplay = new SwitchDisplay(autoButton,
				autoButtonPos, MECHANISM_NAMES[AUTO_BUTTON]);
		// TODO RPC
		// we will need to make an array that is passed in

		/**
		 * Mechanism buttons : started and initialized
		 */
		MountedPosition[] buttonPositions = new MountedPosition[NUM_MECHS];
		SimSwitch[] buttons = new SimSwitch[NUM_MECHS];
		SwitchDisplay[] buttonDisplays = new SwitchDisplay[NUM_MECHS];
		for (int i = 0; i < buttonPositions.length; i++) {
			buttonPositions[i] = new MountedPosition(RootMount.get(),
					SWITCH_POSITIONS[i][X], SWITCH_POSITIONS[i][Y], 0);
			buttons[i] = new SimSwitch(switchDisplay, buttonPositions[i]);
			buttonDisplays[i] = new SwitchDisplay(buttons[i],
					buttonPositions[i], MECHANISM_NAMES[i]);
		}
		/**
		 * Solenoids
		 */

		MountedPosition[] solenoidPoses = new MountedPosition[NUM_MECHS];
		PneumaticActuatorDisplay[] actuators = new PneumaticActuatorDisplay[NUM_MECHS];
		for (int i = 0; i < solenoidPoses.length; i++) {
			solenoidPoses[i] = new MountedPosition(RootMount.get(),
					ACTUATOR_POSIITIONS[i][X], ACTUATOR_POSIITIONS[i][Y],
					ACTUATOR_ORIENTATION[i]);
			actuators[i] = new PneumaticActuatorDisplay(solenoidPoses[i],
					MECHANISM_NAMES[i],
					ACTUATOR_CHARACTERISTICS[i][CHARACTERSTIC_LENGTH],
					ACTUATOR_CHARACTERISTICS[i][CHARACTERSTIC_WIDTH],
					ACTUATOR_CHARACTERISTICS[i][CHARACTERSTIC_EXTEND_LENGTH],
					ACTUATOR_CHARACTERISTICS[i][CHARACTERSTIC_RETRACT_TIME],
					ACTUATOR_CHARACTERISTICS[i][CHARACTERSTIC_CONTRACT_TIME],
					ACTUATOR_DEFAULT_STATE[i]);
		}

		// /**
		// * Safety
		// */
		// HHSafety safety = new HHSafety(masterButton);
		//
		// /**
		// * Mechanism code
		// */
		// IHHSwitch[] switches = new IHHSwitch[NUM_MECHS];
		// HHMechanism[] mechanisms = new HHMechanism[NUM_MECHS];
		//
		// HHController[] controllers = new HHController[NUM_MECHS];
		// for (int i = 0; i < NUM_MECHS; i++) {
		// switches[i] = new HHToggleSwitch(autoButton, buttons[i]);
		//
		// mechanisms[i] = new HHMechanism(new ISolenoid[] { actuators[i],
		// actuators[i] }, safety);
		//
		// controllers[i] = new HHController(mechanisms[i], switches[i]);
		// }
		HauntedHouse h = new HauntedHouse(CRIO_ADDRESS, CRIO_RPC_PORT,
				USB_PORT, houseDisplay, switchDisplay, masterButton,
				SWITCH_RPC_KEYS[MASTER_BUTTON], masterButtonDisplay,
				autoButton, SWITCH_RPC_KEYS[AUTO_BUTTON], autoButtonDisplay,
				buttons, buttonDisplays, SWITCH_RPC_KEYS, actuators,
				ACTUATOR_RPC_KEYS, NUM_MECHS, MECHANISM_WAIT_TIMES);
		h.start();
		h.useSimSwitches();

		// HauntedHouse h = new HauntedHouse(CRIO_ADDRESS, CRIO_RPC_PORT,
		// USB_PORT, houseDisplay, switchDisplay, masterButtonSim,
		// masterButtonRPC, masterButtonDisplay, autoButtonSim, autoButtonRPC,
		// autoButtonDisplay, switchSims, switchRPCs, switchDisplays,
		// actuatorSims, actuatorRPCs, safetyButton, mechanisms, controllers)
	}
}
